package com.xiaomai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaomai.entity.Goods;
import com.xiaomai.service.GoodsService;
import com.xiaomai.util.AuthContextHolder;
import com.xiaomai.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 一件商品也就是一个订单，若有人买，直接往买家id字段插入id即可
 */
@Api(tags = "商品信息接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //查看商品信息
    @ApiOperation("查看商品信息")
    @GetMapping("{id}")
    public Result getGoodsInfo(
            @PathVariable String id
    ){
        Goods goods = goodsService.getById(id);
        return Result.ok(goods);
    }

    //删除商品
    @ApiOperation("删除商品")
    @DeleteMapping("{id}")
    public Result delete(
            @PathVariable String id
    ){
        goodsService.removeById(id);
        return Result.ok();
    }

    //购买了的订单查询
    @ApiOperation("购买的订单信息")
    @GetMapping("/buyList")
    public Result<Page> buyList(
            @RequestParam Integer pageNum
            ,@RequestParam Integer pageSize
            ,String search
            , HttpServletRequest request
    ){
        Page<Goods> goodsPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getBuyersId, AuthContextHolder.getUserIdToken(request));
        queryWrapper.orderByDesc(Goods::getId);
        Page<Goods> resultPage = goodsService.page(goodsPage, queryWrapper);
        return Result.ok(resultPage);
    }

    //购买商品
    //修改商品信息 --> 主要添加买家id字段就可以了
    @ApiOperation("购买商品信息")
    @PutMapping("buy")
    public Result buy(
            @RequestBody Goods goods
    ){
        boolean isSuccess = goodsService.updateById(goods);
        if(isSuccess){
            return Result.ok().message("购买成功");
        }else{
            return Result.fail().message("购买失败");
        }
    }

    //查询发布的商品
    @ApiOperation("查询发布的商品")
    @GetMapping("/pulish")
    public Result<Page> pulish(
            @RequestParam Integer pageNum
            , @RequestParam Integer pageSize
            , HttpServletRequest request
    ){
        Page<Goods> goodsPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getUserId, AuthContextHolder.getUserIdToken(request));
        queryWrapper.orderByDesc(Goods::getId);
        Page<Goods> resultPage = goodsService.page(goodsPage, queryWrapper);
        return Result.ok(resultPage);
    }

    //修改商品信息
    @ApiOperation("修改商品信息")
    @PutMapping("update")
    public Result update(
            @RequestBody Goods goods
    ){
        boolean isSuccess = goodsService.updateById(goods);
        if(isSuccess){
            return Result.ok();
        }else{
            return Result.fail().message("修改失败");
        }
    }

    //查询商品集合
    @ApiOperation("检索商品信息")
    @GetMapping("/list")
    public Result<Page> list(
            @RequestParam Integer page
            ,@RequestParam Integer pageSize
            ,String name
            , HttpServletRequest request
    ){
        Page<Goods> goodsPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like(Goods::getGoodsName,name);
        }
        //去除自己发布的
        queryWrapper.ne(Goods::getUserId,AuthContextHolder.getUserIdToken(request));
        //状态为0，表示没被购买
        queryWrapper.eq(Goods::getStatus, 0);
        //逆序
        queryWrapper.orderByDesc(Goods::getId);

        Page<Goods> resultPage = goodsService.page(goodsPage, queryWrapper);
        return Result.ok(resultPage);
    }

    //发布商品
    @ApiOperation("发布商品")
    @PostMapping("/publish")
    public Result<Goods> register(
            @RequestBody Goods goods,
            HttpServletRequest request
            ){
        String userId = AuthContextHolder.getUserIdToken(request);
        goods.setUserId(userId);
        //保存商品
        boolean isSuccess = goodsService.save(goods);
        if(isSuccess){
            //成功则返回用户信息--自动生成的id会映射到传进去的对象中
            return Result.ok(goods);
        }else{
            return Result.fail();
        }
    }

}
